package progress.exa32_1.metaAnnotations.inherited;

import java.lang.annotation.*;
import java.lang.reflect.AnnotatedElement;

public class InheritedTest {
    /**
     * 使用元注解Inherited
     */
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    @Inherited
    private @interface WithInherited {
    }

    /**
     * 不使用元注解Inherited
     */
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    private @interface NoInherited {
    }

    @WithInherited
    @NoInherited
    public static class SuperClass {

    }

    public static class ChildClass extends SuperClass {

    }

    public static void main(String[] args) {
        AnnotatedElement element = SuperClass.class;
        System.out.println("SuperClass 是否被 @WithInherited 注解? " + element.isAnnotationPresent(WithInherited.class));
        System.out.println("SuperClass 是否被 @NoInherited 注解? " + element.isAnnotationPresent(NoInherited.class));
        AnnotatedElement childElement = ChildClass.class;
        System.out.println("ChildClass 是否继承 SuperClass 中的 @WithInherited 注解? " + childElement.isAnnotationPresent(WithInherited.class));
        System.out.println("ChildClass 是否继承 SuperClass 中的 @NoInherited 注解? " + childElement.isAnnotationPresent(NoInherited.class));
    }

}
